﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum CircleSize
{
    size_1 = 1,
    size_2 = 2,
    size_3 = 3,
    size_4 = 4,
    size_5 = 5,
    size_6 = 6,
    size_7 = 7,
    size_8 = 8,
    size_9 = 9,
}
/// <summary>
/// 默认为Ready;
/// 待命以及鼠标正在控制小球位置时,StandBy;
/// 跌落时,Dropping;
/// 碰撞到其他物体后,Collision
/// </summary>
public enum CircleState
{
    Ready = 0,
    StandBy = 1,    
    Dropping = 2,   //下落中
    Collision = 3,  //碰撞发生后
}

public class Circle : MonoBehaviour
{
    public CircleSize circleSize = CircleSize.size_1;
    public CircleState circleState = CircleState.Ready;

    public float limit_x = 0f;
    public float limit_y = 0f;

    private bool IsMove = false;
    private Vector3 originalScale;      //原始尺寸
    public float scaleSpeed = 0.01f;    //尺寸恢复的速度
    public int score = 0;               //该小球的分数

    private void Awake()
    {
        originalScale = this.transform.localScale;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //游戏状态为Release时，销毁小球
        if (GameManager.gmInstance.gameState == GameState.Release)
        {
            GameManager.gmInstance.DelCIrcle(this.gameObject);
            return;
        }

        //当游戏状态与小球状态都为StandBy时才进行移动小球位置的操作
        if (GameManager.gmInstance.gameState == GameState.StandBy && circleState == CircleState.StandBy)
        {
            if (Input.GetMouseButtonDown(0))
            {
                IsMove = true;
            }
            //松开鼠标时
            if (Input.GetMouseButtonUp(0) && IsMove)
            {
                IsMove = false;
                //松开鼠标，改变重力,让圆圈掉落
                this.gameObject.GetComponent<Rigidbody2D>().gravityScale = 2.0f;
                //改变小球状态
                circleState = CircleState.Dropping;
                //改变游戏状态
                GameManager.gmInstance.gameState = GameState.InProgress;

                //创建新的水果
                //GameManager.gmInstance.CreateNewCircle();
                GameManager.gmInstance.InvokeCreateNewCircle(0.5f);
            }
            if (IsMove)
            {
                //移动位置
                //ScreenToWorldPoint将鼠标在屏幕上的坐标转化成在游戏相内的坐标
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector3 new_CirPos = new Vector3(mousePos.x, this.gameObject.GetComponent<Transform>().position.y, this.gameObject.GetComponent<Transform>().position.z);
                this.gameObject.GetComponent<Transform>().position = new_CirPos;

                //对x位置进行约束,避免超出边界
                float x = this.transform.position.x;
                if (x > limit_x)
                {
                    this.transform.position = new Vector3(limit_x, this.transform.position.y, this.transform.position.z);
                }
                if (x < -limit_x)
                {
                    this.transform.position = new Vector3(-limit_x, this.transform.position.y, this.transform.position.z);
                }
            }
        }

        //如果小球已经碰撞过,并且y左边超出limit_y，说明已经触碰红线，游戏结束
        if (this.circleState == CircleState.Collision && this.transform.position.y >= this.limit_y)
        {
            if (GameManager.gmInstance.gameState < GameState.GameOver)
            {
                GameManager.gmInstance.gameState = GameState.GameOver;
                Debug.Log("Game Over!");
            }
        }

        //尺寸恢复
        if (this.transform.localScale.x < originalScale.x)
        {
            this.transform.localScale += originalScale * scaleSpeed;
        }
    }

    //合并检测，检测是否会发生合并
    private bool TryCombine(Collision2D collision)
    {
        bool flag = false;
        //碰撞到Circle
        if (collision.gameObject.tag.Contains("Circle"))
        {
            //如果撞到等大的小球，就进行合并，并且不能是最大尺寸
            if (circleSize == collision.gameObject.GetComponent<Circle>().circleSize && circleSize < CircleSize.size_9)
            {
                flag = true;
                //为了避免碰撞的双方都调用合成函数,去约束让y坐标小的调用函数
                //y相等时x小的去调用
                float ax, ay, bx, by;
                ax = this.transform.position.x;
                ay = this.transform.position.y;
                bx = collision.transform.position.x;
                by = collision.transform.position.y;
                if (ay < by || (ay == by && ax < bx))
                {
                    //合成小球并让它由小变大
                    GameManager.gmInstance.CombineNewCircle(circleSize + 1, this.transform.position, collision.transform.position);

                    //删掉原来的两个小球
                    GameManager.gmInstance.DelCIrcle(this.gameObject);
                    GameManager.gmInstance.DelCIrcle(collision.gameObject);
                }
            }
        }
        return flag;
    }

    //监测碰撞
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("collision");

        //下落中的小球发生了碰撞且碰撞到的是其他小球或者地板,则需要更新该小球和游戏的状态
        if (circleState == CircleState.Dropping)
        {
            //判断是否碰到了其他小球或者地板
            if (collision.gameObject.tag.Contains("Floor") || collision.gameObject.tag.Contains("Circle"))
            {
                circleState = CircleState.Collision;
                GameManager.gmInstance.gameState = GameState.StandBy;

                GameManager.gmInstance.AddScore(score);

                //尝试和碰撞的物体合并
                TryCombine(collision);

                if (collision.gameObject.tag.Contains("Floor"))
                {
                    GameManager.gmInstance.hitSource.Play();
                }
            }
        }
        //已经碰撞后的小球再次发生碰撞
        else if (circleState == CircleState.Collision)
        {
            //尝试和碰撞的物体合并
            TryCombine(collision);
        }

    }
}
